package com.ydtg.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.ydtg.bean.Typeface;
import com.ydtg.dao.BaseDao;
import com.ydtg.dao.TypefaceDao;

public class TypefaceDaoImpl extends BaseDao implements TypefaceDao{
	public List<Typeface> getall(Typeface news) {
		// TODO 自动生成的方法存根
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Typeface> list = new ArrayList<Typeface>();
		
		try {
			conn = getconnection();
			String sql = "select id,name,wordprice from typeface where 1=1";
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Typeface pet  = new Typeface();
				pet.setId(rs.getInt(1));
				pet.setName(rs.getString(2));
				pet.setWordprice(rs.getInt(3));
				list.add(pet);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			this.closeAll(conn, stmt, rs);
		}
		
		return list;
	}

	@Override
	public int add(Typeface pet) {
		
		String psql = "insert into typeface(name,wordprice) "
				+ "values(?,?)";
		Object[] params = {pet.getName(),pet.getWordprice()};
		
		int count=this.executeUpdate(psql, params);
		return count;
	}

	@Override
	public int updateByID(Typeface pet) {
		
		String psql = "	UPDATE typeface set name=?,wordprice=? where id=?";
		Object[] params = {pet.getName(),pet.getWordprice(),pet.getId()};
		int count =  this.executeUpdate(psql, params);
		return count;
	}

	@Override
	public int delByID(int id) {
		String psql = "	delete from typeface where id=?";
		Object[] params = {id};
		int count = this.executeUpdate(psql, params);
		return count;
	}

	@Override
	public List<Typeface> getTypefaceList(Typeface pet) {
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Typeface> list = new ArrayList<Typeface>();
		try {
			conn = getconnection();
			String psql = "select id,name,wordprice from typeface where 1=1";
			if (pet!=null&&pet.getId()!=0) {
				psql = psql+" and id="+pet.getId();
			}
			if (pet!=null&&pet.getName()!=null&&!"".equals(pet.getName())) {
				psql = psql+" and name="+pet.getName();
			}
			if (pet!=null&&pet.getWordprice()!=0) {
				psql = psql+" and wordprice="+pet.getWordprice();
			}
			stmt = conn.prepareStatement(psql);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Typeface pet1  = new Typeface();
				pet1.setId(rs.getInt(1));
				pet1.setName(rs.getString(2));
				pet1.setWordprice(rs.getInt(3));
				list.add(pet1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			closeAll(conn, stmt, null);
		}
		return list;
	}
}
